package cn.stronglink.asset.message.mq.dam.handle;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.dam.entity.IScreenCommandFactory;
import cn.stronglink.asset.message.mq.dam.entity.ScreenCommandEntity;
import cn.stronglink.asset.message.mq.dam.entity.dacai.UpdateArrayTextCommandFactory;
import cn.stronglink.asset.message.mq.dam.message.MQMessageOfDAM;
import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.dam.service.DamService;
import cn.stronglink.asset.module.dam.service.IDamService;
import cn.stronglink.suite.core.util.ByteUtil;
import cn.stronglink.suite.core.util.ContextUtils;

public class OpenAssetDetailPageHandle extends AbstractReturnExceptionHandle{
	protected final static String TOPIC_NAME = "serviceToDam";
	protected final static String ACTION_CODE = "screenRequest";
	protected final static String SEND_ACTION_CODE= "screenCommand";
	protected IDamService damService =(DamService)ContextUtils.getBean("sensorService");
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	private final static short ASSET_TITLE_ID = 1;
	private final static short ASSET_CONTENT_ID = 2;
	private final static short ASSET_SCREEN_ID =5 ;
	@Override
	public boolean isHandle(MQMessageOfDAM msg) {
		if(!msg.getActioncode().equals(ACTION_CODE)) return false;
		ScreenCommandEntity sc = (ScreenCommandEntity) msg.getAwsPostdata();
		if(sc.getCommand().indexOf("EEB1110003")==0&&filtration(sc.getCommand())) {
			return true;
		}
		return false;
	}
	
	@Override
	protected void handleMessage(MQMessageOfDAM msg) {
		ScreenCommandEntity sc = (ScreenCommandEntity) msg.getAwsPostdata();
		String command = sc.getCommand();
		String rfid = new String(ByteUtil.hexStringToBytes(command.substring(14, command.length()))).trim();
		ItAssetVo itAssetVo = damService.qryAssetInfo(rfid);
		if(itAssetVo!=null) {
			try {
				this.sendAssetInfoToScreen(itAssetVo, sc.getRackConverCode());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
			Map<Short,String> map = new HashMap<>();
			map.put(ASSET_TITLE_ID, "");
			map.put(ASSET_CONTENT_ID, "");
			try {
				this.send(sc.getRackConverCode(),map);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	private void sendAssetInfoToScreen(ItAssetVo itAssetVo,String rackConverCode) throws Exception {
		Map<Short,String> map = new HashMap<>();
		map.put(ASSET_TITLE_ID, itAssetVo.getAssetName());
		StringBuffer sb = new StringBuffer();
		sb.append(itAssetVo.getUseBranchName()==null?"":itAssetVo.getUseBranchName()+"\t\t"
				+itAssetVo.getEndUserName()==null?"":itAssetVo.getEndUserName()+"\r\n");
//		sb.append("资产类型:"+itAssetVo.getTypeName()+"\r\n");
//		sb.append("资产品牌:"+itAssetVo.getBrandName()+"\r\n");
//		sb.append("资产型号:"+itAssetVo.getModelName()+"\r\n");
		sb.append("电话:"+itAssetVo.getContactsPhone()==null?"":itAssetVo.getContactsPhone()+"\r\n");
		sb.append("IP地址"+itAssetVo.getIp()==null?"":itAssetVo.getIp()+"\r\n");
		sb.append("编号:"+itAssetVo.getAssetNo()==null?"":itAssetVo.getAssetNo()+"\r\n");
		sb.append("SN:"+itAssetVo.getSerialNo()==null?"":itAssetVo.getSerialNo()+"\r\n");
//		sb.append("RFID号:"+itAssetVo.getRfidNo()+"\r\n");
		sb.append("起始U位:"+itAssetVo.getStartU()+"\r\n");
		sb.append("结束U位:"+itAssetVo.getEndU()+"\r\n");
		map.put(ASSET_CONTENT_ID, sb.toString());
		this.send(rackConverCode, map);
	}
	
	private void send(String rackConverCode,Map<Short,String> map) throws Exception {
		IScreenCommandFactory rackScreenCommandFactroy = new UpdateArrayTextCommandFactory(rackConverCode, ASSET_SCREEN_ID, map);
		ScreenCommandEntity sc =  rackScreenCommandFactroy.createScreenCommand();
		
		MQMessageOfDAM message = new MQMessageOfDAM();
		message.setActioncode(SEND_ACTION_CODE);
		message.setAwsPostdata(sc);
		message.setTimestamp(System.currentTimeMillis());
		
		String json = JSON.toJSONString(message);
		topicSender.send(TOPIC_NAME,json);
		logger.info("topic:{}  发送命令:{}",TOPIC_NAME,json);
	}
	
	private boolean filtration(String commad) {
		if(commad.length()<14)return false;
		int controlId = Integer.parseInt(commad.substring(10, 14),16);
		System.out.println("========="+controlId);
		for(int i=0;i<9;i++) {
			if(controlId==6*i+4) {
				return true;
			}
		}
		return false;
	}
	
}

